package runnable_functions;

import gui.Main;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import details.*;

/**
 * @author Tami
 *
 */
public class GETfunctions extends RunnableClasses{
	
	public GETfunctions(){}
	
	public class GetTop5 implements Runnable {

		private ArrayList<Cd> top5cds;
		private ArrayList<Song> top5songs;
		private ArrayList<Artist> top5artists;
		private CountDownLatch startSignal;
		
		public GetTop5(CountDownLatch startSignal){
			this.startSignal = startSignal;
		}
		@Override
		public void run() {
			try{
				Main.db.openConnection();
				top5cds= Main.db.getTOP_5_CDs();
				top5songs = Main.db.getTOP_5_Songs();
				top5artists = Main.db.getTOP_5_Artists();
				Main.guiInt.updateTopArtist(top5artists);
				Main.guiInt.updateTopCds(top5cds);
				Main.guiInt.updateTopSongs(top5songs);
				
	
			}
			catch(SQLException e){
				Main.guiInt.notifyDBFailure(e.toString());
			}
			finally{
				Main.db.closeConnection();
				startSignal.countDown();
			
			}
			
		}
		
	}
	
	
	
	
	public class IsUserLike implements Runnable {

		private String target_id;
		private Target target;
		private int userID;
		private CountDownLatch startSignal;
		
		public IsUserLike(String target_id, Target target, int userID,CountDownLatch startSignal){
			this.startSignal = startSignal;
			this.target = target;
			this.target_id= target_id;
			this.userID = userID;
		}
		@Override
		public void run() {
			try{
				Main.db.openConnection();
				boolean isUserLike = Main.db.isUserLike(target_id, target, userID);
				System.out.println("the answer is "+isUserLike);
				Main.guiInt.isIfUserLike(isUserLike);
				
			}
			catch(SQLException e){
				Main.guiInt.notifyDBFailure(e.toString());
			}
			finally{
				Main.db.closeConnection();
				startSignal.countDown();
			
			}
			
		}
		
	}

}
